﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Weight.Base;
using Weight.Base.Utility;
using Weight.Dal;
using Weight.Model;

namespace Weight.Bll
{
    public class BllMenu
    {
        /// <summary>
        /// 获取菜单项
        /// </summary>
        /// <param name="name">查询名称</param>
        /// <returns></returns>
        public IEnumerable<MenuEntity> GetMenu(string name)
        {
           return  new DalMenu().GetMenu(name);
        }

        /// <summary>
        /// 通过父节点获取菜单项
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public IEnumerable<MenuEntity> GetMenuByPId(string pid)
        {
            return new DalMenu().GetMenusByPId(pid);
        }

        /// <summary>
        /// 根据Id获取菜单项
        /// </summary>
        /// <param name="id">菜单ID</param>
        /// <returns></returns>
        public MenuEntity GetMenuById(string id)
        {
            return new DalMenu().GetMenuById(id);
        }


        /// <summary>
        /// 获取第一级的菜单项
        /// </summary>
        /// <returns></returns>
        public IEnumerable<MenuEntity> GetFirstLevelMenu(int level)
        {
            return new DalMenu().GetFirstLevelMenu(level);
        }

        /// <summary>
        /// 添加菜单项
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public  bool Add(MenuEntity entity)
        {
            try
            {
                return new DalMenu().Add(entity);
            }
            catch(Exception ex)
            {
                LogHelper.WriteLog("添加菜单项",ex);
                return false;
            }
        }

        /// <summary>
        /// 判断菜单名称是否已经存在
        /// </summary>
        /// <param name="MenuName">菜单名称</param>
        /// <returns></returns>
        public bool  NameIsExist(string MenuName,string pName)
        {
            return new DalMenu().NameIsExist(MenuName, pName);
        }

        /// <summary>
        /// 修改菜单
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Update(MenuEntity entity)
        {
            return new DalMenu().Update(entity);
        }

        /// <summary>
        ///不同编号的菜单名称是否已经存在
        /// </summary>
        /// <param name="name">名称</param>
        /// <returns></returns>
        public bool MenuNameIsExist(string name,string pName, string menuId)
        {
            var data = new DalMenu().GetMenuByName(name, pName, menuId);
            if (data.Count() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除菜单项包括子菜单
        /// </summary>
        /// <param name="id">菜单编号</param>
        /// <returns></returns>
        public bool DeleteById(string id)
        {
            return new DalMenu().DeleteById(id);
        }

        /// <summary>
        /// 初始化菜单
        /// </summary>
        /// <returns></returns>
        public void  InitMenu()
        {
            try
            {
                var dal = new DalMenu();
                var data = dal.GetMenuByPId("/");
                Random rand = new Random((int)DateTime.Now.Ticks);
                List<MenuEntity> list = new List<MenuEntity>();
                if (data.Count() == 0)
                {
                    var rootEntity = new MenuEntity()
                    {
                        Id = DateTime.Now.ToString("yyyyMMddHHmmss") + rand.Next(999).ToString().PadLeft(3, '0'),
                        PId = "/",
                        MenuName = "根节点",

                        IsShow = true,
                        Sort = 0
                    };
                    var MenuNode = new MenuEntity()
                    {
                        Id = DateTime.Now.ToString("yyyyMMddHHmmss") + rand.Next(999).ToString().PadLeft(3, '0'),
                        PId = rootEntity.Id,
                        MenuName = "系统设置",
                        IsShow = true,
                        Sort = 0
                    };

                    var subNode = new MenuEntity()
                    {
                        Id = DateTime.Now.ToString("yyyyMMddHHmmss") + rand.Next(999).ToString().PadLeft(3, '0'),
                        PId = MenuNode.Id,
                        MenuName = "菜单管理",
                        PageName = "WeightSystem.MenuManager",
                        IsShow = true,
                        Sort = 0
                    };

                    list.Add(rootEntity);
                    list.Add(MenuNode);
                    list.Add(subNode);
                }
                else
                {
                    var data1 = dal.GetMenuByPId(data.First().Id).Where(p=>p.MenuName== "系统设置");
                    if (data1.Count() == 0)
                    {
                        var MenuNode = new MenuEntity()
                        {
                            Id = DateTime.Now.ToString("yyyyMMddHHmmss") + rand.Next(999).ToString().PadLeft(3, '0'),
                            PId = data.First().Id,
                            MenuName = "系统设置",
                            IsShow = true,
                            Sort = 0
                        };

                        var subNode = new MenuEntity()
                        {
                            Id = DateTime.Now.ToString("yyyyMMddHHmmss") + rand.Next(999).ToString().PadLeft(3, '0'),
                            PId = MenuNode.Id,
                            MenuName = "菜单管理",
                            PageName = "WeightSystem.MenuManager",
                            IsShow = true,
                            Sort = 0
                        };

                        list.Add(MenuNode);
                        list.Add(subNode);
                    }
                    else
                    {
                        var data2 = dal.GetMenuByPId(data1.First().Id);
                        if (data2.Count() == 0)
                        {
                            var subNode = new MenuEntity()
                            {
                                Id = DateTime.Now.ToString("yyyyMMddHHmmss") + rand.Next(999).ToString().PadLeft(3, '0'),
                                PId = data1.First().Id,
                                MenuName = "菜单管理",
                                PageName = "WeightSystem.MenuManager",
                                IsShow = true,
                                Sort = 0
                            };
                            list.Add(subNode);
                        }
                    }
                }

                if (list.Count > 0)
                {
                    dal.Add(list);
                }
            }
            catch(Exception ex)
            {
                LogHelper.WriteLog("初始化菜单",ex);
            }
            
        }
    }
}
